#include <gtest/gtest.h>

using namespace ::std;

class Solution {
public:
    int lengthOfLastWord(string s);
};

// 求最后一个单词的长度，不是最长的单词的长度
// 暴力法Brutal-Force
// 优化方向，反向遍历，因为只要求最后一个单词的长度！！！
int Solution::lengthOfLastWord(string s) {
	int lastLength = 0, currentLength = 0;
	for(char c : s) {
		if(c == ' ') {
			if(currentLength > 0) lastLength = currentLength;
			currentLength = 0;
		} else {
			currentLength++;
		}
	}
	return currentLength == 0 ? lastLength : currentLength;
}

class LengthOfLastWordCheck : public ::testing::Test {
public:
	class Solution solution;
};

TEST_F(LengthOfLastWordCheck, testLengthOfLastWord_1) {
	string s = "Hello World";
	EXPECT_TRUE(5 == solution.lengthOfLastWord(s));
}

TEST_F(LengthOfLastWordCheck, testLengthOfLastWord_2) {
	string s = "   fly me   to   the moon  ";
	EXPECT_TRUE(4 == solution.lengthOfLastWord(s));
}

TEST_F(LengthOfLastWordCheck, testLengthOfLastWord_3) {
	string s = "luffy is still joyboy";
	EXPECT_TRUE(6 == solution.lengthOfLastWord(s));
}

int main() {
	::testing::InitGoogleTest();
	return RUN_ALL_TESTS();
}
